Imports MyClasses
Imports System.Data
Imports System.Data.SqlClient

Public Class DTab
    Public Shared Function GetTab() As DataTable
        Dim strSql As String = ""
        Try
            strSql = "SELECT TabID,TabNm,TabDesc,TabOrd,TabStatus " & _
                        "FROM mPrefTab "
            Return DbHelper.ExecuteDataTable(strSql)
        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "DAL", "DTab", "GetTab", strSql)
            Return Nothing
        End Try
    End Function

    Public Shared Function GetControls() As DataTable
        Dim StrSql As String = ""
        Try
            StrSql = "SELECT TabID,PrefID,PrefName,PrefValue,PrefDesc,PrefCtl,PrefDataType,PrefSrc,PrefSrcQry,PrefMinVal," & _
                     "PrefMaxVal,PrefCtlMaxlen,Update_yn, isnull(KeyName,'') as KeyName,Compulsory_yn,tooltip FROM mPrefs WHERE PrefStatus = 'A' order by TabId desc"
            Return DbHelper.ExecuteDataTable(StrSql)
        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "DAL", "DTab", "GetControls", StrSql)
            Return Nothing
        End Try
    End Function

    Public Shared Function GetSelectRecord(ByVal sql) As DataTable
        Try
            Return DbHelper.ExecuteDataTable(sql)
        Catch ex As Exception
            DGeneral.ErrorHandler(ex.Message, "DAL", "DTab", "GetSelectRecord", sql)
            Return Nothing
        End Try
    End Function

    Public Shared Function Update(ByVal tab() As CTab, ByVal PrefID As Integer) As Boolean
        Dim Param(2) As SqlParameter
        Dim strSql As String = ""
        Dim intCount As Integer
        Dim dtGetData As DataTable
        Dim dbConnection As New dbAccess
        Try
            dbConnection.BeginTransaction()
            dtGetData = DTab.GetControls()

            For intCount = 0 To dtGetData.Rows.Count - 1
                If Not IsNothing(tab(intCount)) Then
                    PrefID = dtGetData.Rows(intCount).Item("PrefID")
                    strSql = "Update mPrefs set PrefValue=@PrefVal, LD=@LD where Update_yn=@updateyn and PrefId=" & PrefID
                    Param(0) = New SqlParameter("@PrefVal", tab(intCount).PrefValue)
                    Param(1) = New SqlParameter("@LD", Now.Date())
                    Param(2) = New SqlParameter("@updateyn", "Y")
                    dbConnection.ExecuteNonQuery(strSql, Param)
                End If
            Next
            dbConnection.CommitTransaction()
            Return True
        Catch ex As Exception
            dbConnection.RollBackTransaction()
            DGeneral.ErrorHandler(ex.Message, "DAL", "DTab", "Update", strSql)
        End Try
    End Function
End Class
